﻿using Finbuckle.MultiTenant;
using Finbuckle.MultiTenant.Abstractions;
using Finbuckle.MultiTenant.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using MultiTenant.ShareDb.Api.Models;

namespace MultiTenant.ShareDb.Api.Data;

public class ApplicationDbContext : MultiTenantDbContext
{
    public ApplicationDbContext(IMultiTenantContextAccessor multiTenantContextAccessor) 
        : base(multiTenantContextAccessor)
    {
    }

    public ApplicationDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options)
        : base(multiTenantContextAccessor, options)
    {
    }
    
    public DbSet<Product> Products { get; set; }
    
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        
        // Add multi-tenant configuration to Product entity
        builder.Entity<Product>().ToTable("Products");
        builder.Entity<Product>().Property(p => p.Id).ValueGeneratedOnAdd();
        builder.Entity<Product>().Property(p => p.Name).HasMaxLength(100).IsRequired();
        builder.Entity<Product>().IsMultiTenant();
    }
}